package cn.bjit.controller;

import cn.bjit.controller.request.SendCodeRequest;
import cn.bjit.enums.BizCodeEnum;
import cn.bjit.enums.SendCodeEnum;
import cn.bjit.service.NotifyService;
import cn.bjit.util.CommonUtil;
import cn.bjit.util.JsonData;
import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/api/notify/v1")
public class NotifyController {
    @Autowired
    private NotifyService notfiyService;
    @Autowired
    private Producer captchaProducer;
    @Autowired
    private StringRedisTemplate redisTemplate;

    //redis过期时间 1O分钟
    private static final long CAPTCHA_CODE_EXPIRED=1000*10*60;




    /**
     * 发送图形验证码
     */
    @GetMapping("captcha")
     public  void getCaptcha(HttpServletRequest request,HttpServletResponse response){
         String captchaText=captchaProducer.createText();
         log.info("验证码====={}",captchaText);
         //存到redis中
         redisTemplate.opsForValue().set(this.getCaptchKey(request),captchaText,CAPTCHA_CODE_EXPIRED,TimeUnit.MILLISECONDS);

         BufferedImage   bufferedImage=captchaProducer.createImage(captchaText);
         try( ServletOutputStream outputStream = response.getOutputStream()) {
             ImageIO.write(bufferedImage,"jpg",outputStream);
             outputStream.flush();
         } catch (IOException e) {
             e.printStackTrace();
             log.error("获取流出错{}",e.getMessage());
         }
     }

    private String getCaptchKey(HttpServletRequest request) {
        String ipAddr = CommonUtil.getIpAddr(request);
        String userAgent=request.getHeader("User-Agent");
        String key="account-service:captcha:"+CommonUtil.MD5(ipAddr+userAgent);
        log.info("图形验证码的key={}",key);
        return  key;
    }
    /**
     * 短信验证码
     * @return
     */
    @PostMapping("send_code")
    public JsonData sendCode(@RequestBody SendCodeRequest sendCodeRequest,HttpServletRequest request) {
         String key=this.getCaptchKey(request);  //拿到redis中key
         String captchaText = redisTemplate.opsForValue().get(key);  //根据key获取redis中的 4位图形验证码
        String captcha = sendCodeRequest.getCaptcha(); //用户输入的图形验证码

        if(captcha!=null&&captchaText!=null &&captcha.equalsIgnoreCase(captchaText)){ //成功
            redisTemplate.delete(key);
            //发送短信验证码
            JsonData jsonData=notfiyService.sendCode(SendCodeEnum.USER_REGISTER,sendCodeRequest.getTo());
            return jsonData;
        }else{
            return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA_ERROR);
        }
    }
}
